package org.qcit.plugin.tasks;

import com.android.build.gradle.api.BaseVariant;
import com.google.gson.Gson;


import org.gradle.api.DefaultTask;
import org.gradle.api.Project;
import org.gradle.api.tasks.TaskAction;
import org.qcit.plugin.Extension;
import org.qcit.plugin.api.DingTalkInterface;
import org.qcit.plugin.api.RetrofitUtil;

import java.io.IOException;

import okhttp3.ResponseBody;
import retrofit2.Response;


/**
 * 发送消息到钉钉
 */
public class SendMsgToDingTalkTask extends DefaultTask {
    public BaseVariant variant;
    public Project project;
    public void init(BaseVariant variant,Project project){
        this.variant = variant;
        this.project = project;
        setDescription("发送消息到钉钉");
        setGroup("打包上传");
    }
    public static class ActionCard{
        public String title;
        public String text;
        public int hideAvatar = 0;
        public int btOrientation = 0;
        public String singleTitle;
        public String singleUrl;
    }
    public static class DingMsg{
        public ActionCard actionCard = new ActionCard();
        public String msgType ="actionCard";
    }
    @TaskAction
    public void sendMsg(){
        System.out.println("===================sendMsg========================：" );
        Extension extension = Extension.getConfig(project);
        DingMsg dingMsg = new DingMsg();
        dingMsg.actionCard.title = extension.msgTitle;
        dingMsg.actionCard.text = extension.msgContent;
        dingMsg.actionCard.singleTitle = extension.singleButtonTitle;
        dingMsg.actionCard.singleUrl = extension.singleButtonUrl;
        System.out.println("发送消息了");
//        try {
//            Response<ResponseBody> appResponse = RetrofitUtil.
//                    getRetrofit("https://oapi.dingtalk.com/")
//                    .create(DingTalkInterface.class)
//                    .sendMsg(extension.apiToken,dingMsg).execute();
//            System.out.println("appResponse: " + new Gson().toJson(appResponse.body().toString()));
//        }catch (IOException e) {
//            e.printStackTrace();
//        }
    }
}
